<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use app\admin\model\Admin as AdminModel;
use app\admin\model\System as SystemModel;
use app\admin\validate\Admin as AdminValidate;

class Sign extends Controller
{

    /**
     * 登录显示
     */
    public function login()
    {
        //是否显示验证码
        $is_show_yzm = false;
        if(session('login_num') >= 3){
            $is_show_yzm = true;
        }
        $this->assign('is_show_yzm', $is_show_yzm);
        return $this->fetch();
    }

    /**
     * @param Request $request
     * 登录检测
     */
    public function check(Request $request)
    {
        //dump($request->param());die;

        //统计登录次数
        session('login_num',session('login_num') + 1);

        $validate = new AdminValidate;

        if(session('login_num') >= 3){
            if(!$validate->scene('login')->check($request->param())){
                $this->error($validate->getError());
            }
        }else{
            if(!$validate->scene('login_no_yzm')->check($request->param())){
                $this->error($validate->getError());
            }
        }

        $tel = $request->param('tel');
        $password = $request->param('admin_password');
        //echo $username . '|' . $password;die;
        $admin = AdminModel::getByTel($tel);
        //dump($admin);die;
        if($admin){
            if($password == $admin->admin_password){
                //登录次数归零
                session('login_num', 0);
                //用户信息存session
                session('id', $admin->admin_id);
                session('tel', $admin->tel);
                session('role_id', $admin->admin_role);
                //跳转
                //$this -> success('登录成功！',url('Index/index'));
                switch ($admin->admin_role)
                {
                    case 1:
                        $this->redirect('admin/Index/index');
                        break;
                    case 101:
                        $this->redirect('admin/Ncjy/add');
                        break;
                    case 102:
                        $this->redirect('admin/Pic/add');
                        break;
                    case 103:
                        $this->redirect('admin/Pic/add');
                        break;
                    default:
                        return '系统错误，请与管理员联系!';
                }

            }else{
                //密码错误
                $this -> error('用户名或密码错误...');
            }
        }

        //什么也不是
        $this -> error('用户名或密码错误...');
    }

    /**
     * 注销退出
     */
    public function logout(){
        //清除session
        session(null);
        //跳转到登录页面
        //$this -> success('退出成功',url('login'),3);
        $this->redirect('admin/Sign/login');exit;
    }


    /**
     * @param Request $request
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function pwReset(Request $request){
        if($request->isPost()){
            //处理提交数据
            $password = input('post.password');
            $newpwd = input('post.newpwd');
            $admin = AdminModel::get(session('id'));
            //dump($admin);die;
            $oldpwd = $admin->admin_password;
            if($oldpwd == $password){
                //echo 'ok';die;
                $admin->admin_password = $newpwd;
                $result = $admin->save();
                //dump($result);die;
                if($result){
                    $this -> success('密码修改成功请重新登录！','admin/Sign/logout');
                }else{
                    $this -> error('密码修改失败！');
                }
            }else{
                $this -> error('原密码错误！');
            }
        }else{
            return $this->fetch();
        }
    }

    /*
     * 注册用户
     */
    public function reg0(Request $request)
    {
        //判断能否注册
        $system = SystemModel::get(1);
        if(!$system->reg_on){
            return '注册已关闭!';
        }
        if($request->isPost()){
            $validate = new AdminValidate;
            if(!$validate->scene('reg')->check($request->param())){
                $this->error($validate->getError());
            }
            //添加
            $admin = new AdminModel;
            //allowField(true)过虑post数组中非数据表字段数据
            $admin->allowField(true)->save($request->param());
            $this->success('注册成功，请登录后上传信息！','admin/sign/login');
        }
        $roles = get_com_role();
        $this->assign('roles', $roles);
        return $this->fetch();
    }

    // 上传成功提示
    public function info()
    {
        return $this->fetch();
    }


}
